import { SSE } from "sse.js";
import R from "ramda";

const connect = R.curryN(_connect, 4);

function _connect(url, headers, data, onMessage, onError) {
  this.sse = new SSE(url, { headers }, data);
  this.sse.addEventListener("message", onMessage);
  this.sse.addEventListener("error", onError);

  return sse;
}
_connect.prototype.sse;
_connect.prototype.close = function () {
  this.sse.close();
};

function createSSE(onMessage, onError = R.identity) {
  return connect(R.__, R.__, R.__, onMessage, onError);
}
export default createSSE;
